package com.lkm.app_ui.m.Imple.fun;

import android.content.Context;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelyardValueEvent;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;



class SaveSteelyardValue {

    private static Observer observer;

    public static void start() {

        if (observer != null) return;

        SteelyardDataModule mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        SteelyardValue steelyardValue = mSteelyardDataModule.getSteelyardValue(RunTimeData.steelyardInfo.getSteelyardMac());
        if(steelyardValue!=null){
            String json =  GsonHelp.toJson(steelyardValue);
            AppConst.getApplication().getSharedPreferences("netCache", Context.MODE_PRIVATE).edit().putString("SteelyardValue", json).apply();
        }

        LinkBus.registerReceiver(observer = new ObserverAutoNext<OnSteelyardValueEvent>() {
            @Override
            public void onReceiver(OnSteelyardValueEvent data) {
                String json =  GsonHelp.toJson(data.getSteelyardValue());
                AppConst.getApplication().getSharedPreferences("netCache", Context.MODE_PRIVATE).edit().putString("SteelyardValue",json).apply();
            }

            @Override
            public Class getBusEventClass() {
                return OnSteelyardValueEvent.class;
            }
        });
    }
}
